﻿using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

public class ResultShowUI : MonoBehaviour
{
    [Header("实验配置")]
    public InputField inf_Duration;
    public InputField inf_Inv;
    public InputField inf_CountDownTime;
    public InputField inf_Frquency;

    public Text tex_ResourceLoadedStat;


    [Header("参与者信息")]
    public InputField inf_Index;
    public InputField inf_Name;
    public InputField inf_Age;

    public Toggle tog_MaleGender;
    public Toggle tog_ASD;


    [Header("注视点展示")]
    public Image ImageDisplay;
    public Text ImageIdx;
    public GameObject pointSprite;
    public GameObject[] pointsCarrier;

    [Header("按钮")]
    public Button ReturnBtn;
    public Button DeleteBtn;
    public Button LeftArrow;
    public Button RightArrow;

    private int indexOfDisplayed = 0;
    private bool checkMode = false;

    public void Start()
    {
        checkMode = DataIO.handlingPath.Length == 0;
        DeleteBtn.interactable = !DataIO.handlingPath.Contains(DataIO.rawJsonDataBasePath);

        inf_Duration.interactable = false;
        inf_Inv.interactable = false;
        inf_CountDownTime.interactable = false;
        inf_Frquency.interactable = false;

        inf_Index.interactable = false;
        inf_Name.interactable = false;
        inf_Age.interactable = false;
        tog_MaleGender.interactable = false;
        tog_ASD.interactable = false;

        ImageDisplay.preserveAspect = true;

        inf_Duration.text = Serializer.serializableCfgInfo.s_ImageDuration.ToString();
        inf_Inv.text = Serializer.serializableCfgInfo.s_IntvDuration.ToString();
        inf_CountDownTime.text = Serializer.serializableCfgInfo.s_CountDown.ToString();
        inf_Frquency.text = Serializer.serializableCfgInfo.s_Frequency.ToString();
        tex_ResourceLoadedStat.text = Serializer.serializableCfgInfo.s_StimulusGroupIdx.ToString();

        inf_Index.text = Serializer.serializableParticipantInfo.s_Index.ToString();
        inf_Name.text = Serializer.serializableParticipantInfo.s_Name.ToString();
        inf_Age.text = Serializer.serializableParticipantInfo.s_Age.ToString();
        tog_MaleGender.isOn = Serializer.serializableParticipantInfo.s_IsMale;
        tog_ASD.isOn = Serializer.serializableParticipantInfo.s_IsASD;

        ImageIdx.text = "Image: " + Serializer.serializableCfgInfo.s_StimulusIdxList[indexOfDisplayed];
        ImageDisplay.sprite = ProcessHandles.Cfg_Info.s_StimulusList[indexOfDisplayed];

        LeftArrow.interactable = indexOfDisplayed > 0;
        RightArrow.interactable = indexOfDisplayed < (Serializer.serializableCfgInfo.s_StimulusIdxList.Count - 1);

        pointsCarrier = new GameObject[Serializer.serializableCfgInfo.s_StimulusIdxList.Count];

        EnablePointVisualize();
    }


    public void OnLeftArrowBtnClicked()
    {
        EnablePointVisualize(false);
        ImageIdx.text = "Image: " + Serializer.serializableCfgInfo.s_StimulusIdxList[--indexOfDisplayed];
        ImageDisplay.sprite = ProcessHandles.Cfg_Info.s_StimulusList[indexOfDisplayed];

        EnablePointVisualize();

        LeftArrow.interactable = indexOfDisplayed > 0;
        RightArrow.interactable = indexOfDisplayed < (Serializer.serializableCfgInfo.s_StimulusIdxList.Count - 1);
    }

    public void OnRightArrowBtnClicked()
    {
        EnablePointVisualize(false);
        ImageIdx.text = "Image: " + Serializer.serializableCfgInfo.s_StimulusIdxList[++indexOfDisplayed];
        ImageDisplay.sprite = ProcessHandles.Cfg_Info.s_StimulusList[indexOfDisplayed];

        EnablePointVisualize();

        LeftArrow.interactable = indexOfDisplayed > 0;
        RightArrow.interactable = indexOfDisplayed < (Serializer.serializableCfgInfo.s_StimulusIdxList.Count - 1);
    }

    public void EnablePointVisualize(bool enabled = true)
    {
        if (Serializer.ss.Length <= indexOfDisplayed)
            return;

        if (pointsCarrier[indexOfDisplayed] == null)
        {
            pointsCarrier[indexOfDisplayed] = new GameObject("pointsCarrier" + indexOfDisplayed.ToString());
            for (int i = 0; i < Serializer.ss[indexOfDisplayed].Count; i++)
            {
                GameObject p = Instantiate(pointSprite, pointsCarrier[indexOfDisplayed].transform);
                p.transform.position = ImageDisplay.transform.TransformPoint(Serializer.ss[indexOfDisplayed][i]);
            }
        }
        pointsCarrier[indexOfDisplayed].SetActive(enabled);
    }

    public void OnReturnBtnCilcked()
    {
        if (checkMode)
        {
            DataIO.WriteSerializedData(true);
            Serializer.AddParticipant();
        }

        SceneManager.LoadScene(0);
    }

    public void OnDeleteBtnCilcked()
    {
        if (checkMode)
        {
            SceneManager.LoadScene(0);
        }
        else
        {
            DataIO.DeleteHandlingFile();
        }
    }
}



